package com.jf.blog_jf04.controller;

import com.jf.blog_jf04.data.ov.ApiCommentOV;
import com.jf.blog_jf04.service.BlogService;
import com.jf.blog_jf04.service.CommentService;
import com.jf.blog_jf04.service.TimelineService;
import com.jf.blog_jf04.service.UserinfoService;
import com.jf.blog_jf04.utils.LoginUtils;
import com.jf.blog_jf04.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/api")
@RestController
@Api(tags = "前端通用接口")
public class ApiController {

    @Autowired
    private UserinfoService userinfoService;
    @Autowired
    private BlogService blogService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private TimelineService timelineService;

    @GetMapping("/vip/info")
    @ApiOperation("博主信息")
    public R vipInfo() {
        return userinfoService.getVipInfo();
    }

    @GetMapping("/blog/page")
    @ApiOperation("首页博客列表")
    public R blogPage(@ApiParam(value = "当前页", required = true, defaultValue = "1", example = "1") @RequestParam int page) {
        return blogService.getVipPage(page, null);
    }

    @GetMapping("/blog/category/page")
    @ApiOperation("分类博客列表")
    public R blogCategoryPage(
            @ApiParam(value = "当前页", required = true, defaultValue = "1", example = "1")
            @RequestParam
            int page,
            @ApiParam("分类编号")
            @RequestParam(required = false)
            Integer categoryId) {
        return blogService.getVipPage(page, categoryId);
    }

    @GetMapping("/blog/tag/page")
    @ApiOperation("标签博客列表")
    public R blogTagPage(
            @ApiParam(value = "当前页", required = true, defaultValue = "1", example = "1")
            @RequestParam
            int page,
            @ApiParam("分类编号")
            @RequestParam(required = false)
            Integer tagId) {
        return blogService.getVipPageByTagId(page, tagId);
    }

    @GetMapping("/blog/info/{blogId}")
    @ApiOperation("博客详情")
    public R blogInfo(@ApiParam("博客编号") @PathVariable int blogId) {
        return blogService.getVipBlog(blogId);
    }


    @GetMapping("/comment/page/{blogId}")
    @ApiOperation("评论分页")
    public R commentPage(@ApiParam(value = "博客编号", required = true) @PathVariable int blogId, @ApiParam(value = "当前页", required = true, defaultValue = "1", example = "1") @RequestParam int page) {
        return commentService.getVipCommentPage(page, blogId);
    }

    @PostMapping("/comment/save")
    @ApiOperation("发表评论")
    public R commentSave(@RequestBody ApiCommentOV form) {
        commentService.saveComment(form);
        return R.success();
    }

    @GetMapping("/timeline")
    @ApiOperation("时间轴数据")
    public R timeline() {
        return timelineService.getTimeline(LoginUtils.getVipId());
    }

    @GetMapping("/timeline/more/{page}")
    @ApiOperation("加载更多")
    public R timelineMore(@ApiParam(value = "当前页", defaultValue = "2", example = "2") @PathVariable int page, @ApiParam(value = "年份", required = true) @RequestParam String year) {
        return timelineService.getBlogPageByYear(page, LoginUtils.getVipId(), year);
    }

}
